#!/usr/local/bin/php
<?php

function openalsource_state_english($state) {
	switch($state) {
		case(OpenAL::STATE_INITIAL): {
			return 'initial';
		}
		case(OpenAL::STATE_PLAYING): {
			return 'playing';
		}
		case(OpenAL::STATE_PAUSED): {
			return 'paused';
		}
		case(OpenAL::STATE_STOPPED): {
			return 'stopped';
		}
		default: {
			return 'er.';
		}
	}
}



//. load the file into an openal buffer.
if(file_exists($file = '/opt/code/repo/php-openal/test/commopen.wav')) {
	echo("Loading file: {$file}...\n");
	$buffer = OpenALBuffer::newFromFile($file);
} else {
	die("unable to locate file: {$file}\n");
}

printf(
	"\nBuffer Details:\n\tBits: %d\n\tChannels: %d\n\tFrequency: %d\n\tSize: %d\n",
	$buffer->bits,
	$buffer->channels,
	$buffer->frequency,
	$buffer->size
);



//. create a new sound source.
$source = new OpenALSource;
$source->buffer = $buffer;

list($px,$py,$pz) = $source->position;
list($vx,$vy,$vz) = $source->velocity;
printf(
	"\nSource Details:\n\tGain: %f\n\tPosition: %f, %f, %f\n\tVelocity: %f, %f, %f\n".
	"\tLooping: %d\n\tPitch: %f\n\tState: %s\n",
	$source->gain,
	$px, $py, $pz,
	$vx, $vy, $vz,
	$source->loop,
	$source->pitch,
	openalsource_state_english($source->state)
);

//. play the sound.
$source->play();
if($source->state == OpenAL::STATE_PLAYING) {
	echo("\nSource is playing.\n");
}

//. sleeping so we can hear it play. in a PHP-GTK app Gtk::main would take care
//. of the nessessary uptime (obviously (i think)).
sleep(2);



//. free openal resources.
$buffer->free();
$source->free();

?>